const initialState = { saveDir: [], curDir: "" };
const dirReducer = (state = initialState, action) => {
  if (action.type === "saveDir") {
    const newState = {
      curDir: action.params?.curDir,
      saveDir: [...state.saveDir, action.params?.pidDir],
    };
    localStorage.setItem("dirState", JSON.stringify(newState));
    return newState;
  }
  if (action.type === "unsaveDir") {
    const newState = {
      curDir: action.params?.curDir,
      saveDir: action.params?.saveDir,
    };
    localStorage.setItem("dirState", JSON.stringify(newState));
    return newState;
  }
  if (action.type === "clearDir") {
    const newState = {
      curDir: "",
      saveDir: [],
    };
    localStorage.removeItem("dirState");
    return newState;
  }
  const dirState = JSON.parse(localStorage.getItem("dirState"));
  return dirState ? dirState : state;
};
export default dirReducer;
